package com.plum.admin.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plum.admin.common.annotation.Log;
import com.plum.admin.common.enums.LogType;
import com.plum.admin.common.util.R;
import com.plum.admin.config.security.permission.MySecurityMetadataSource;
import com.plum.admin.modules.sys.dto.RoleDTO;
import com.plum.admin.modules.sys.dto.RoleMenuDTO;
import com.plum.admin.modules.sys.entity.SysRole;
import com.plum.admin.modules.sys.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色管理")
@RequiredArgsConstructor
public class SysRoleController extends BaseController {

    private final ISysRoleService roleService;
    private final MySecurityMetadataSource mySecurityMetadataSource;
    @GetMapping
    @PreAuthorize("hasAuthority('sys:role:list')")
    @ApiOperation("分页查询角色列表")
    @Log(type = LogType.OPERATION,title = "分页查询角色列表")
    public R<Object> list(Integer page, Integer pageSize, SysRole role){
        return R.ok(roleService.findRoleList(new Page<SysRole>(page,pageSize),role));
    }
    @GetMapping("/getRoleList")
    @PreAuthorize("hasAuthority('sys:role:list')")
    @Log(type = LogType.OPERATION,title = "查询角色列表")
    @ApiOperation("查询角色列表")
    public R<Object> list(){
        return R.ok(roleService.list());
    }
    @PostMapping("/saveRolePermission")
    @ApiOperation("设置角色权限")
    @Log(type = LogType.OPERATION,title = "设置角色权限")
    public R<Object> saveRolePermission(@RequestBody RoleMenuDTO roleMenuDTO){
        boolean flag = roleService.saveRolePermission(roleMenuDTO);
        mySecurityMetadataSource.loadResourceDefine();
        return R.ok(flag);
    }
    @PostMapping
    @ApiOperation("保存角色")
    @Log(type = LogType.OPERATION,title = "保存角色")
    @PreAuthorize("hasAuthority('sys:role:save')")
    public R<Object> saveRole(@RequestBody RoleDTO roleDTO){
        return R.ok(roleService.insertRole(roleDTO));
    }

    @PutMapping
    @ApiOperation("修改角色")
    @Log(type = LogType.OPERATION,title = "修改角色")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public R<Object> updateRole(@RequestBody RoleDTO roleDTO){
        return R.ok(roleService.updateRole(roleDTO));
    }

    @DeleteMapping("{ids}")
    @ApiOperation("删除角色")
    @Log(type = LogType.OPERATION,title = "删除角色")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public R<Object> deleteRole(@PathVariable("ids") String ids){
        List<String> roleIdList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(roleService.deleteRole(roleIdList));
    }
}
